package com.dkd.manage.service.Impl;

import com.dkd.manage.constant.MessageConstant;
import com.dkd.manage.exception.CheckException;
import com.dkd.manage.mapper.EmpMapper;
import com.dkd.manage.mapper.RegionMapper;
import com.dkd.manage.mapper.RoleMapper;
import com.dkd.manage.pojo.dto.EmpQueryListDto;
import com.dkd.manage.pojo.entity.Emp;
import com.dkd.manage.pojo.entity.Region;
import com.dkd.manage.pojo.entity.Role;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.EmpService;
import com.dkd.manage.utils.RegexUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class EmpServiceImpl implements EmpService {


    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private RoleMapper roleMapper;


    /**
     * 分页查询人员列表
     * @param empQueryListDto
     * @return
     */
    @Override
    public PageResult<Emp> list(EmpQueryListDto empQueryListDto) {
        //开启分页
        PageHelper.startPage(empQueryListDto.getPageNum(), empQueryListDto.getPageSize());
        //查询区域列表
        List<Emp> empList = empMapper.list(empQueryListDto);
        //创建分页详情对象
        PageInfo<Emp> pageInfo = new PageInfo<>(empList);
        //封装数据返回
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增区域
     * @param emp
     */
    @Override
    public Integer add(Emp emp) {
        //校验手机号格式是否正确
        if(RegexUtils.isPhoneInvalid(emp.getMobile())) {
            throw new CheckException(MessageConstant.PHONE_IS_NOT_CORRECT);
        }
        //根据区域id查询区域
        Region region = regionMapper.getRegionById(emp.getRegionId());
        //根据角色id查询角色
        Role role = roleMapper.selectRoleByRoleId(emp.getRoleId());
        emp.setRegionName(region.getRegionName());
        emp.setRoleName(role.getRoleName());
        emp.setRoleCode(role.getRoleCode());
        return empMapper.add(emp);
    }

    /**
     * 修改人员
     * @param emp
     * @return
     */
    @Override
    public Integer edit(Emp emp) {
        //校验手机号格式是否正确
        if(RegexUtils.isPhoneInvalid(emp.getMobile())) {
            throw new CheckException(MessageConstant.PHONE_IS_NOT_CORRECT);
        }
        //根据区域id查询区域
        Region region = regionMapper.getRegionById(emp.getRegionId());
        //根据角色id查询角色
        Role role = roleMapper.selectRoleByRoleId(emp.getRoleId());
        emp.setRegionName(region.getRegionName());
        emp.setRoleName(role.getRoleName());
        emp.setRoleCode(role.getRoleCode());
        return empMapper.edit(emp);
    }

    /**
     * 根据id查询人员
     * @param id
     * @return
     */
    @Override
    public Emp getEmpById(Long id) {
        return empMapper.getEmpById(id);
    }

    /**
     * deleteRegionById
     * @param ids
     * @return
     */
    @Override
    public Integer deleteEmpById(Long[] ids) {

        return empMapper.deleteEmpById(ids);
    }
}
